/*
 * @Descripttion: 患者注册账号
 * @version: 1.0
 * @Author: 自由如风
 * @Date: 2021-04-28 17:46:15
 * @LastEditors: 自由如风
 * @LastEditTime: 2021-04-28 18:26:34
 */

const DB = require("../../lib/DB");
const { Success } = require("../../lib/Response");


module.exports = async function (ctx, next) {
    try {
        const { name, pwd, sex } = ctx.request.body;

        if (!!(await DB.models.user.findOne({ where: { name } }))) {
            throw `用户名[${name}]已被占用`
        }

        const user = await DB.models.user.create({
            name, pwd, sex,
            identity: 3,
            creat_time: Date.now(),
        })

        ctx.body = new Success(user.toJSON())
        await next()
    } catch (e) {
        if (typeof e == 'string') {
            throw new Error(e);
        } else {
            throw new Error("注册失败")
        }
    }
}